package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.ResetPwdBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.PwdUtil;

/**
 * 重置密码 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("pwdReset")
public class PwdReset extends AbstractProcessor<Object, ResetPwdBo> implements Processor<Object, ResetPwdBo> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    /**
     * 校验
     */
    @Override
    public void check(ResetPwdBo bo) throws OceanException {
    	Long userId = bo.getUserId();
    	if (userId == null) {
    		throw new OceanException(ExceptionCode.用户ID为空);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(ResetPwdBo param) throws OceanException {
    	
    	Long userId = param.getUserId();
    	User record = new User();
    	record.setUserId(userId);
    	record.setPwd(PwdUtil.md5(PwdUtil.init()));
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	userDao.updateByPrimaryKeySelective(record);
    	
    	String name = ((DetailUserVo) userRead.detail(userId)).getName();
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(ResetPwdBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.重置密码.key());
    	record.setTargetId(param.getUserId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
